const jwt = require('jsonwebtoken');
const fs = require('fs');
const privateKey = fs.readFileSync('./public/rsa/private.pem');
const publicKey = fs.readFileSync('./public/rsa/public.pem');


// 使用私钥对 JWT 进行签名（加密）
function signJWT(payload) {
    return jwt.sign(payload, privateKey, { expiresIn: '24h', algorithm: 'RS256' });
}

// 使用公钥对 JWT 进行验证和解密
function verifyJWT(token) {
    return new Promise((resolve, reject) => {
        jwt.verify(token, publicKey, (err, decoded) => {
            if (err) {
                // 验证失败
                reject('Token验证失败');
            } else {
                // 验证成功
                resolve(decoded);
            }
        });
    });
}

module.exports = {
    signJWT,
    verifyJWT
};